<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: 'index' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>咨询公告管理</el-breadcrumb-item>
		</el-breadcrumb>

		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true" :model="queryData">
				<el-form-item label="公告标题">
					<el-input v-model="queryData.announcementName" @clear="queryList" clearable
						placeholder="请输入公告标题"></el-input>
				</el-form-item>
				<el-form-item label="公告内容">
					<el-input v-model="queryData.announcementContent" @clear="queryList" clearable
						placeholder="请输入公告内容"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="add">新增公告</el-button>
					<el-button type="danger" @click="removBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>

		<!-- 数据表格区域 -->
		<el-table :data="dataList" border style="width: 100%" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID" width="180"></el-table-column>
			<el-table-column prop="announcementName" label="公告标题" width="180"></el-table-column>
			<el-table-column prop="announcementContent" label="公告内容"></el-table-column>
			<el-table-column prop="announcementPublishtime" label="发布时间" width="180"></el-table-column>
			<el-table-column prop="img" label="公告封面" width="200">
				<template v-slot="scope">
					<el-image :src="'http://localhost:8080/sfzy'+scope.row.img">
						<div slot="error" class="image-slot">
							<i class="el-icon-picture-outline"></i>
						</div>
					</el-image>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" @click="edit(scope.row)">编辑</el-button>
					<el-button size="mini" type="danger" @click="remove(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryData.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryData.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>

		<!--  
			el-dialog: 对话框组件
				title: Dialog 的标题
				visible: 是否显示 Dialog，支持 .sync 修饰符
		-->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="addOrUpdateData" label-width="100px" ref="addOrUpdateForm">
				<el-form-item label="公告标题">
					<el-input v-model="addOrUpdateData.announcementName" placeholder="请输入公告标题"></el-input>
				</el-form-item>
				<el-form-item label="公告封面">
					<el-upload action="http://localhost:8080/sfzy/fileUpload" list-type="picture-card"
						:on-remove="handleRemove" :on-success="handleAvatarSuccess" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
				<el-form-item label="公告内容">
					<el-input type="textarea" autosize placeholder="请输入内容"
						v-model="addOrUpdateData.announcementContent">
					</el-input>
					<!-- <div class="text item">
						<quill-editor v-model="addOrUpdateData.announcementContent"></quill-editor>
					</div> -->
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="confirm">确 定</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				// 查询参数对象
				queryData: {
					announcementName: '',
					announcementContent: '',
					pageNum: 1,
					pageSize: 10
				},
				// 状态列表
				options: [{
					value: '1',
					label: '正常'
				}, {
					value: '2',
					label: '停用'
				}],
				total: 0,
				title: '新增用户', // 对话框的标题
				dataList: [], // 当前页数据列表
				dialogFormVisible: false,
				// 新增和修改数据集
				addOrUpdateData: {
					id: "",
					announcementName: "",
					announcementContent: "",
					img: "",
				},
				ids: [],
				dialogImageUrl: '',
				dialogVisible: false,
				fileList: []
			}
		},
		methods: {
			// 这个是图片上传的对话框
			handleSuccess(res, file) {
				this.addOrUpdateData.img = res.data;
			},
			handleRemove(file, fileList) {
				console.log(file, fileList);
			},
			handleAvatarSuccess(response) {
				this.addOrUpdateData.img = response.data
			},
			handleSelectionChange(val) {
				// val是选中的数组对象, map() 方法返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值。
				this.ids = val.map(item => item.id);
			},
			removBatch() {
				if (this.ids.length == 0) {
					this.$message('请选择要删除的数据!');
				} else {
					this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.$ajax.delete('sysAnnouncement?ids=' + this.ids).then(resp => {
							if (resp.code == 200) {
								this.$message.success(resp.message)
								// 重新查询
								this.queryList()
							} else {
								this.$message.error(resp.message)
							}

						})

					}).catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					});
				}
			},
			// 每页条目数改变的回调
			handleSizeChange(val) {
				// 修改查询参数的pageSize
				this.queryData.pageSize = val;
				// pageSize改变，默认查询第一页
				this.queryData.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			// 页码发生改变的回调
			handleCurrentChange(val) {
				// 修改查询参数的pageNum
				this.queryData.pageNum = val;
				// 重新查询
				this.queryList()
			},
			// 封装查询用户列表数据的函数
			queryList() {
				// 因为自定义axios已经定义了baseUrl，所以这里直接写相对路径即可
				this.$ajax.get('sysAnnouncement/page', {
					params: this.queryData
				}).then(resp => {
					if (resp.code == 200) {
						this.dataList = resp.data.list
						this.total = resp.data.total
					} else {
						this.$message.error('用户数据加载失败')
					}
				})
			},
			// 对话框的取消按钮事件函数: 清空对话的数据
			cancle() {
				this.dialogFormVisible = false
				this.addOrUpdateData = {
					id: "",
					announcementName: "",
					announcementContent: "",
				};
				this.fileList = []
			},
			// 对话框的确定按钮事件函数
			confirm() {
				this.dialogFormVisible = false

				if (this.addOrUpdateData.id == "") { // 执行新增请求
					this.$ajax.post('sysAnnouncement', this.addOrUpdateData).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}
					})
				} else {
					this.$ajax.put('sysAnnouncement', this.addOrUpdateData).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}
					})
				}
				this.cancle()
			},
			// 新增用户
			add() {
				this.dialogFormVisible = true;
				this.title = '新增公告';
				this.fileList = [];
			},
			// 修改用户
			edit(row) {
				this.title = '编辑公告'
				// 根据id实时查询数据，然后进行回显
				this.$ajax.get('sysAnnouncement/' + row.id).then(resp => {
					this.addOrUpdateData = resp.data
					this.addOrUpdateData.id = row.id;
					this.fileList = [{
						name: this.addOrUpdateData.img.substr(this.addOrUpdateData.img.lastIndexOf('\\') +
							1),
						// xdasd.jpg
						url: 'http://localhost:8080/sfzy' + this.addOrUpdateData.img
					}]
				})
				this.dialogFormVisible = true;
			},
			// 删除用户
			remove(row) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$ajax.delete('sysAnnouncement/' + row.id).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}

					})

				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		},
		// 页面初始化的时候，加载第一页的数据
		mounted() {
			this.queryList()
		}
	}
</script>

<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		border-bottom: 1px solid #eee;
		padding-bottom: 15px;
	}

	.el-pagination {
		margin-top: 15px
	}


	.el-col>.el-select {
		width: 100%;
	}


	.text {
		font-size: 14px;
	}

	.item {
		padding: 18px 0;
	}


	.ql-container {
		min-height: 200px;
	}

	.thead .cell {
		text-align: center;
	}
</style>